package sfeir.gwt.ergosoom.server.service;


import java.util.Calendar;

import sfeir.gwt.ergosoom.client.model.Profile;
import sfeir.gwt.ergosoom.server.util.Pair;

import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.images.Image;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.appengine.api.images.Transform;
//import com.google.inject.Singleton;

//@Singleton
public class ProfileServiceImpl implements ProfileService {
    ProfileAccess service = new ProfileAccess();
    private DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();

    @Override
    public Profile getProfile(String alias) {
        return service.get(alias);
    }

    @Override
    public String saveProfilePicture(String mime, byte[] data) {
        Image image = ImagesServiceFactory.makeImage(data);
        Transform resize = ImagesServiceFactory.makeResize(80, 100);
        ImagesService imgService = ImagesServiceFactory.getImagesService();
        Image newImage = imgService.applyTransform(resize, image);
        String id = Long.toHexString(Calendar.getInstance().getTimeInMillis());
        Entity entity = new Entity("Picture");//fetchProfileEntity(userService.getCurrentUser().getEmail());
        entity.setProperty("pictureEmail", id);
        entity.setProperty("pictureMime", mime);
        entity.setProperty("pictureData", new Blob(newImage.getImageData()));
        String url = id + ".png";
        entity.setProperty("image", url);
        datastoreService.put(entity);
//        MemcacheServiceFactory.getMemcacheService().delete(userService.getCurrentUser().getEmail());
        return url;
    }

    @Override
    public Pair<byte[], String> getProfilePicture(String id) {
        Entity entity = fetchPictureEntity(id);
        if (null == entity)
            return null;
        Blob bytes = (Blob) entity.getProperty("pictureData");
        String mime = (String) entity.getProperty("pictureMime");
        if (null == bytes)
            return null;
        return new Pair<byte[], String>(bytes.getBytes(), mime);
    }

    private Entity fetchPictureEntity(String id) {
        Entity entity = null;//fetchProfileEntity(email);
        Query q = new Query("Picture").addFilter("pictureEmail", FilterOperator.EQUAL, id);
        entity = datastoreService.prepare(q).asSingleEntity();
        return entity;
    }

    @Override
    public Profile login(String login, String password) {
        Profile p = service.get(login);
        if (p != null) {
            String pass = service.getPassword(login);
            if (pass != null && pass.equals(password)) {
                p.setPassword(null);
                return p;
            }
        }
        return null;
    }

    @Override
    public void saveProfile(Profile profile) {
        service.save(profile);
        profile.setPassword(null);
    }

    @Override
    public Boolean checkAlias(String alias) {
       return (new ProfileAccess().getPassword(alias) == null);
    }
}